

public void controlEvent(ControlEvent theEvent) 
{
  int   vValue = 0; 
  int   vRegId = 0;

  // Debug.log("Event: "+ theEvent.name() + " C:" + theEvent.isController() );


  if(theEvent.isGroup())
  {
    vRegId = int(theEvent.group().name());
    if( vRegId >= 0 && vRegId < gNumberOfRegs )
    {
      for(int i=0;i<theEvent.group().arrayValue().length;i++) {
        int n = (int)theEvent.group().arrayValue()[i];
        vValue += pow(2, i) * n;
      }    

      mRegisters[ vRegId ].set( vValue );
    }
  }

  if(theEvent.isController())
  {
    vValue = round(theEvent.value());


    Debug.log("got an event from ID: "+ theEvent.controller().id()+ ", Value: " + vValue);



    /*    if( theEvent.controller().id() >= 100 && theEvent.controller().id() <= 200 ) // numberBox
     {  
     mRegisters[ theEvent.controller().id() - 100 ].set( vValue );
     mRegisters[ theEvent.controller().id() - 100 ].updateCheckBox();
     }
     else
     */
    if( theEvent.controller().id() >= 300 && theEvent.controller().id() <= 360 ) // waveforms
    {
      if( theEvent.controller().id() >= 310 && theEvent.controller().id() <= 320 ) // waveform 1 
      {
        mRegisters[ 4 ].setBit( theEvent.controller().id() - 310, vValue ); 
        mRegisters[ 4 ].updateCheckBox();
      }
      if( theEvent.controller().id() >= 320 && theEvent.controller().id() <= 330 ) // waveform 2
      {
        mRegisters[ 11 ].setBit( theEvent.controller().id() - 320, vValue ); 
        mRegisters[ 11 ].updateCheckBox();
        
      }
      if( theEvent.controller().id() >= 330 && theEvent.controller().id() <= 340 ) // waveform 3
      {
        mRegisters[ 18 ].setBit( theEvent.controller().id() - 330, vValue ); 
        mRegisters[ 18 ].updateCheckBox();        
      }
      if( theEvent.controller().id() == 351) // PWM 1
      {
        mRegisters[ 2 ].set( vValue & 0xFF ); 
        mRegisters[ 3 ].set( (vValue & 0xF00) >> 8 ); 
        mRegisters[ 2 ].updateCheckBox();        
        mRegisters[ 3 ].updateCheckBox();        
      }
      if( theEvent.controller().id() == 352) // PWM 2
      {
        mRegisters[ 9 ].set( vValue & 0xFF ); 
        mRegisters[ 10 ].set( (vValue & 0xF00) >> 8 ); 
        mRegisters[ 9 ].updateCheckBox();        
        mRegisters[ 10 ].updateCheckBox();        
      }
      if( theEvent.controller().id() == 353) // PWM 3
      {
        mRegisters[ 16 ].set( vValue & 0xFF ); 
        mRegisters[ 17 ].set( (vValue & 0xF00) >> 8 ); 
        mRegisters[ 16 ].updateCheckBox();        
        mRegisters[ 17 ].updateCheckBox();        
      }
      
    }
    else // Slider
    {
      switch( theEvent.controller().id() ) {
        case(0): //OSC1
        mRegisters[0].set( vValue - 256 * ( int( vValue / 256 ) ) );
        mRegisters[0].updateCheckBox();
        mRegisters[1].set( int( vValue / 256 ) ); 
        mRegisters[1].updateCheckBox();
        break;
        case(7): //OSC2
        mRegisters[7].set( mOscLB[vValue] );
        mRegisters[7].updateCheckBox();
        mRegisters[8].set( mOscHB[vValue] );
        mRegisters[8].updateCheckBox();
        break;
        case(14): //OSC3
        mRegisters[14].set( mOscLB[vValue] );
        mRegisters[14].updateCheckBox();
        mRegisters[15].set( mOscHB[vValue] );
        mRegisters[15].updateCheckBox();
        break;

        case(5): // Attack1
        mRegisters[5].setHighNibble( vValue);
        mRegisters[5].updateCheckBox();
        break;
        case(205): // Decay1
        mRegisters[5].setLowNibble( vValue );
        mRegisters[5].updateCheckBox();
        break;
        case(6): // Sustsin1
        mRegisters[6].setHighNibble( vValue );
        mRegisters[6].updateCheckBox();
        break;
        case(206): // Release1
        mRegisters[6].setLowNibble( vValue );
        mRegisters[6].updateCheckBox();
        break;

        case(12): // Attack2
        mRegisters[12].setHighNibble( vValue);
        mRegisters[12].updateCheckBox();
        break;
        case(212): // Decay2
        mRegisters[12].setLowNibble( vValue );
        mRegisters[12].updateCheckBox();
        break;
        case(13): // Sustsin2
        mRegisters[13].setHighNibble( vValue );
        mRegisters[13].updateCheckBox();
        break;
        case(213): // Release2
        mRegisters[13].setLowNibble( vValue );
        mRegisters[13].updateCheckBox();
        break;

        case(19): // Attack3
        mRegisters[19].setHighNibble( vValue);
        mRegisters[19].updateCheckBox();
        break;
        case(219): // Decay3
        mRegisters[19].setLowNibble( vValue );
        mRegisters[19].updateCheckBox();
        break;
        case(20): // Sustsin3
        mRegisters[20].setHighNibble( vValue );
        mRegisters[20].updateCheckBox();
        break;
        case(220): // Release3
        mRegisters[20].setLowNibble( vValue );
        mRegisters[20].updateCheckBox();
        break;
      }
    }
  }
}



// Buttons:
private void Setup( int pValue ) {
  //this.controlP5.window("Setup").show();
  SidSetup(this.controlP5);
}

private void Slider( int pValue ) {
  SliderWindow(this.controlP5);
}

